//
//  ViewController.m
//  UI29_Block
//
//  Created by dllo on 16/1/20.
//  Copyright © 2016年 刘旺辉. All rights reserved.
//

#import "ViewController.h"
#import "TouchView.h"
#import "NetworkingHandler.h"
/** 函数声明 */
int sum(int, int);


/** 函数实现 */
int sum(int x, int y) {
    
    return x + y;
}

int sub(int x, int y) {
    
    return x - y;
}

@interface ViewController ()
@property (weak, nonatomic) IBOutlet TouchView *touchView;
@property (nonatomic, copy) void (^block)(NSString *);
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    [self functionReview];
    [self studyBlock];
    [self handleData];
    
    
    [self.touchView viewDidClick:^(TouchView *view) {
        NSLog(@"ddd");
    }];
    
    [self.touchView buttonHandle:^(UIButton *deleteBtn) {
       
        NSLog(@"点击了删除按键");
    }];
    
    [self.touchView editBtnHandle:^(UIButton *editBtn) {
        
        NSLog(@"点击了编辑按键");
        
    } deleBtnhandle:^(UIButton *deleBtn) {
        NSLog(@"点击了删除 按键");
    }];
    
}


#pragma mark - 数据处理(网络类使用Block回调)
- (void)handleData {
    [NetworkingHandler networkingWithUrl:@"http://api.map.baidu.com/place/v2/search?query=银行&region=大连&output=json&ak=6E823f587c95f0148c19993539b99295" completeHandle:^(id result) {
        
        NSLog(@"%@", result);
    }];
}

#pragma mark - 函数复习
- (void)functionReview {
    /** 函数由三部分: 函数的声明, 函数的实现, 函数的调用. 
     *
     */
    printf("%d", sum(3, 5));
    
    /** 函数指针: 指针指向一块内存, 里面是一段代码段.
     *
     */
    
    /** 定义一个函数指针变量 */
    int (*p)(int, int) = NULL;
    
    // 函数指针变量赋值
    p = sum;
    NSLog(@"%d", p(3, 7));
    //printf("%f", p);
    
    p = sub;
}

#pragma mark - block
- (void)studyBlock {
    /** 
     *  1. Block 和函数指针很相似. 技巧: *p 换成 ^blcok
     *  2. Block 相当于匿名函数, 所以Block变量赋值时, 直接赋函数的实现部分
     */
    
    int (^block1)(int, int) = ^int (int x, int y) {
        
        return x + y;
    };
    
    // 调用block
    block1(3, 6);
    NSLog(@"block1: %d", block1(3, 6));
    
    // block 语法
    
    // 无参无返block
    void (^block2)(void) = ^(void) {
        
    };
    block2();
    void (^block22)() = ^() {
        NSLog(@"无参无返");
    };
    block22();
    
    // 无参有返block
    NSString *(^block3)(void) = ^NSString *(void) {
        
        return @"无参有返";
    };
    
    block3();
    
    NSLog(@"%@", block3());
    
    // 有参无返
    void (^block4)(NSString *, NSString *) = ^(NSString *str1, NSString *str2) {
      
        NSLog(@"%@", [str1 stringByAppendingString:str2]);
    };
    
    block4(@"da", @"lian");
    
    // 有参有返
    NSString *(^block5)(NSString *, NSString *) = ^NSString *(NSString *str1, NSString *str2) {
      
        return [str1 stringByAppendingString:str2];
    };
    
    NSLog(@"block5: %@", block5(@"hello", @"  world!"));
    
    self.block = ^(NSString *str) {
        NSLog(@"%@", str);
    };
}
- (IBAction)handleBlock:(id)sender {
    
    // 调用block
    
    self.block(@"aaa");
}

#pragma mark -
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
